package heliotrope.utils.collection
{
	import flash.utils.Dictionary;
	public dynamic class HashSet extends Dictionary implements ISet
	{
		protected var _size:uint;
		public function HashSet()
		{
			_size = 0;
		}

		public function add(o:Object):void
		{
			if(!contains(o))
			{
				_size++;
				this[o] = o;
			}
		}
		
		public function clear():void
		{
			for(var key:Object in this) delete this[key];
			_size = 0;
		}
		
		public function contains(o:Object):Boolean
		{
			return Boolean(this[o]);
		}

		public function intersect_(s:ISet):void
		{
			for each(var e:Object in s)
			{
				if(this[e])
				{
					delete this[e];
					this._size--;
				}
			}
		}

		public function intersect(s:ISet):ISet
		{
			var tmp:HashSet = this.clone();
			tmp.sub_(s);
			return tmp;
		}
		public function clone():HashSet
		{
			var tmp:HashSet = new HashSet;
			for each(var e:Object in this)
			{
				tmp[e] = e;
			}
			tmp._size = this._size;
			return tmp;
		}
		public function get size():uint
		{
			return _size;
		}
		public function isEmpty():Boolean
		{
			return _size==0;
		}
		public function toArray():Array
		{
			var ar:Array = [];
			for each (var elm:Object in this)
			{
				ar.push(elm);
			}
			return ar;
		}
	}
}
/**
 *Example:
 *	var set:HashSet = new HashSet();
 *	var set2:HashSet = new HashSet();
 *	set.add(1);
 *	set.add(2);
 *	set.add(3);
 *	set2.add(2);
 *	var set3:HashSet = set.sub(set2);
 *	for each(var i:uint in set3)
 *	{
 *		trace(i);
 *	}
 *	trace(set3.size);
 *	set3.clear();
 *	trace(set3.size);
 *	var set4 = set3.clone();
 *	set4.add(10);
 *	trace(set3.contains(10),set4.contains(10));
 * 
 * 	output:
 * 	1
 * 	3
 * 	2
 * 	0
 * 	false true
 **/